package cn.edu.lit.jsjxy.CourseExample.chapter4;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import cn.edu.lit.jsjxy.CourseExample.R;

public class VideoPlayActivity extends Activity implements View.OnClickListener
{
    MediaPlayer mMediaPlayer;
    SurfaceView mSurfaceView;
    Button playBtn;
    String path;
    SurfaceHolder sh;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);

        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
        playBtn = (Button) findViewById(R.id.play1);
        path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/mnt/sdcard/Movies/apple.mp4";

        mMediaPlayer = new MediaPlayer();
        playBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {
        try {
            mMediaPlayer.reset();
            //为播放器对象设置用于显示视频内容、代表屏幕描绘的控制器
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(path);//设置数据源
            sh = mSurfaceView.getHolder();
            mMediaPlayer.setDisplay(sh);
            mMediaPlayer.prepare();
            mMediaPlayer.start();

        } catch (Exception e) {
            Log.i("MediaPlay err", "MediaPlay err");
        }

    }


}
